﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class event_manager : Singleton<event_manager> {

    public delegate void event_handler(string event_name, object udata);

    private Dictionary<string, event_handler> dic = new Dictionary<string, event_handler>();
	
    /// <summary>
    /// 添加事件
    /// </summary>
    /// <param name="event_name"></param>
    /// <param name="h"></param>
    public void add_listener(string event_name, event_handler h)
    {
        if (this.dic.ContainsKey(event_name))
        {
            this.dic[event_name] += h;
        }
        else
        {
            this.dic.Add(event_name, h);
        }
    }


    /// <summary>
    /// 删除事件
    /// </summary>
    /// <param name="event_name"></param>
    /// <param name="h"></param>
    public void remove_listener(string event_name, event_handler h)
    {
        if (!this.dic.ContainsKey(event_name))
            return;
        this.dic[event_name] -= h;
        if(this.dic[event_name] == null)
        {
            this.dic.Remove(event_name);
        }
    }


    /// <summary>
    /// 发布事件
    /// </summary>
    /// <param name="event_name"></param>
    /// <param name="udata"></param>
    public void dispatch_event(string event_name, object udata)
    {
        if (!dic.ContainsKey(event_name))
            return;
        dic[event_name](event_name, udata);
    }
}
